/**============================================================================
 Confidential Information - Limited distribution to authorized persons only.
 This software is protected as an unpublished work under the U.S. Copyright
 Act of 1976.

 Copyright (c) 2012, BluePhoenix Solutions, Inc. All rights reserved.
 ----------------------------------------------------------------------------
 file......: ExceptionFactory.java
 Author....: Vyacheslav Kovalyov
 Created...: 1/19/12

 Description:

 ============================================================================*/
package com.bphx.android.exception;

import android.content.Context;
import com.bphx.android.words.WordExceptionType;

import java.io.FileNotFoundException;
import java.net.SocketTimeoutException;

import static com.bphx.android.R.string.*;

/**
 * @author Vyacheslav Kovalyov
 * @version 1.0
 */
public class ExceptionFactory {

    public static BaseException getException(Exception baseException, Context context) {

        if (baseException instanceof SocketTimeoutException) {

            String[] args = new String[]{
                    context.getString(errorTypeNetwork),
                    context.getString(errorMsgTimeOut)
            };

            String message = context.getString(errorTypedWithMessage, args);
            return new NetworkConnectionException(baseException, message);
        } else if (baseException instanceof FileNotFoundException) {

            String[] args = new String[]{
                    context.getString(errorTypeNetwork),
                    context.getString(errorMsgInvalidUnPwd)
            };

            String message = context.getString(errorTypedWithMessage, args);
            return new NetworkConnectionException(baseException, message);
        }

        return new UnknownException(baseException, context);
    }

    public static WordCreationException getWordCreationException(WordExceptionType type, Context context, String... args) {
        switch (type) {
            case WORD_EXISTS:
                return new WordCreationException(context, errorMsgWordExists);
            case WORD_IS_EMPTY:
                return new WordCreationException(context, errorMsgWordIsEmpty);
            case WORD_HAVE_NOT_VARIANTS:
                return new WordCreationException(context, errorMsgWordHaventVariants);
            case WORD_VARIANTS_ERROR:
                return new WordCreationException(context, context.getString(errorMsgVariantInInvalidVariants, args));
            case WORD_INVALID_VARIANTS_ERROR:
                return new WordCreationException(context, context.getString(errorMsgVariantInVariants, args));
            default:
                throw new RuntimeException("Invalid exception type");
        }
    }
}
